package main.java.com.lee.threadcomm;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/12/22
 */
public class WaitTest {
    public static void main(String[] args) throws InterruptedException {
        Object lock1 = new Object();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(System.in));

        // Reading data using readLine


        Thread waitThread = new Thread(()->{
            try {
                synchronized (lock1) {
                    System.out.println("1加锁成功");
                    System.out.println("1进入阻塞, 释放锁");
                    lock1.wait();
                    System.out.println("1从阻塞中被唤醒");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread notifyThread = new Thread(()->{
            try {
                synchronized (lock1) {
                    String name = reader.readLine();
                    lock1.notify();
                    System.out.println("唤醒waitSet中的线程, 但是此刻还没释放锁");
                    String name2 = reader.readLine();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        waitThread.setName("waitThread");
        notifyThread.setName("notifyThread");


        waitThread.start();
        notifyThread.start();
        waitThread.join();
    }
}
